Skip to content

Implement section attribute#22719

Merged
thewilsonator merged 1 commit intodlang:masterfrom
rikkimax:upstream-section
Mar 20, 2026
Merged

Implement section attribute#22719
thewilsonator merged 1 commit intodlang:masterfrom
rikkimax:upstream-section

Conversation

@rikkimax
Copy link
Copy Markdown
Contributor

Upstreaming this from ldc/gdc/opend.

However I did have to reimplement the flags. They were not correct according to Gemini, and from what I can tell, it is correct.

@rikkimax rikkimax requested a review from ibuclaw as a code owner March 11, 2026 05:29
@dlang-bot
Copy link
Copy Markdown
Contributor

Thanks for your pull request and interest in making D better, @rikkimax! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#22719"

@rikkimax rikkimax force-pushed the upstream-section branch 7 times, most recently from 62be678 to b9deb45 Compare March 11, 2026 06:50
@rikkimax
Copy link
Copy Markdown
Contributor Author

I don't understand the c++ interop tests failing. As far as I know this shouldn't be messing with it.

Windows known.

I've added some debug messages to figure out why my test is failing.

@rikkimax
Copy link
Copy Markdown
Contributor Author

rikkimax commented Mar 11, 2026

ELF is passing.

Windows is asserting out in backend:

assert(strlen(sectname) <= 8); // so it won't go into string_table

Disabled assert, works fine. Seems its more a in progress assert during some development spree and isn't an actual limitation of the code. Now passing requires the $M suffix.

Mach is not passing, I can't get the section start/end symbols to match or generate (I think). Compiler kept adding an underscore, detect is specifically for section$ and only for when using pragma(mangle.

@rikkimax
Copy link
Copy Markdown
Contributor Author

rikkimax commented Mar 11, 2026

I will need to remove my complex 2^^power calculation, its changing alignment size that'll leave holes in the section and really mess up the slices. I will need to emit an error when a type's alignment is not a power of 2.

Expression edtor; // if !=null, does the destruction of the variable
IntRange* range; // if !=null, the variable is known to be within the range

string userDefinedSection; // the @section("name"), if present
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a field needed for this when gdc/ldc do just fine without?

(And the worst kind of field for C++ back-ends)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not.

This is how opend has it implemented, and this was based upon that (but I keep finding "fun things" wrong with it).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Processing during semantic is debatable as Udas are preserved for the codegen pass.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I'm happy to move it. Just haven't done it quite yet. Hopefully it clears up the c++ interop issues, I still have no idea what is causing them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just duplicates information already kept elsewhere, so you're increasing the memory size of all variable/parameter/bitfields by 16 bytes for something 99.99(many 9's later)% of them won't use.

I've just realised that this is only putting global vars into custom sections. Is ignoring functions intentional?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring functions is more like I can't be bothered to come up with a way to verify it in CI.

Realistically people are not likely to use that for dmd.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty hard to use something that doesn't work, yes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only there was a tool that could do object dumps and that same tool can display information for a given section name. :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was using objdump for testing this.

But no, I don't want to mess around with the CI and how to verify it.

@rikkimax rikkimax force-pushed the upstream-section branch 9 times, most recently from a6d8c85 to 94cb8f1 Compare March 11, 2026 22:47
@WalterBright
Copy link
Copy Markdown
Member

What is the purpose of section attributes?

// GDC and LDC declare this attribute in their own modules.
}

/**
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

documentation starts here

@Herringway
Copy link
Copy Markdown
Contributor

What is the purpose of section attributes?

Placing symbols in specific sections. Very useful for embedded applications. I personally use it to guarantee that structures appear at the correct location in ROM images (though the linker does most of the heavy lifting for this).

@rikkimax rikkimax force-pushed the upstream-section branch 2 times, most recently from 87507e0 to f1ea646 Compare March 15, 2026 07:37
@rikkimax
Copy link
Copy Markdown
Contributor Author

When incrementally linking with MSVC's link it'll add padding before merging.

As such, on Windows the type of the variable should be pointers: https://devblogs.microsoft.com/oldnewthing/20190114-00/?p=100695

If you use -g this turns on incremental linking.

Unfortunately this means that linker lists are going to need some more thinking.

@rikkimax rikkimax force-pushed the upstream-section branch 2 times, most recently from 00829e4 to ae3f223 Compare March 15, 2026 11:45
@rikkimax rikkimax marked this pull request as draft March 15, 2026 12:17
@rikkimax rikkimax force-pushed the upstream-section branch 2 times, most recently from 8e46945 to 2b0c683 Compare March 17, 2026 01:14
@rikkimax rikkimax marked this pull request as ready for review March 17, 2026 01:14
@rikkimax
Copy link
Copy Markdown
Contributor Author

I have updated the spec, to include that pragma(mangle does not add the leading underscore on macOS if it begins with section$.

@rikkimax
Copy link
Copy Markdown
Contributor Author

I am happy with current state.

Copy link
Copy Markdown
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably Clang/GCC have the ability to do this too, can you add a C++ test that checks we match what they do?

@rikkimax
Copy link
Copy Markdown
Contributor Author

I've gone ahead and removed the class detection for mutability. Class references (which are what a global variable stores) can be in read-only memory. Its the class instance that can't be, which this doesn't affect.

@thewilsonator thewilsonator merged commit 8c44fed into dlang:master Mar 20, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants